package cn.jy.nvr.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.jy.nvr.datasource.DataSourceConst;
import cn.jy.nvr.datasource.DataSourceContextHolder;
import cn.jy.nvr.domain.NewProvider;
import cn.jy.nvr.domain.ProviderAssay;
import cn.jy.nvr.domain.ProviderBaseInfo;
import cn.jy.nvr.domain.ProviderBodyCheck;
import cn.jy.nvr.domain.ProviderChestCheck;
import cn.jy.nvr.domain.ProviderCollection;
import cn.jy.nvr.domain.ProviderCost;
import cn.jy.nvr.domain.ProviderHeat;
import cn.jy.nvr.domain.ProviderRegister;
import cn.jy.nvr.domain.ProviderSampleAssay;
import cn.jy.nvr.mapper.NewProviderMapper;
import cn.jy.nvr.mapper.ProviderAssayMapper;
import cn.jy.nvr.mapper.ProviderBaseInfoMapper;
import cn.jy.nvr.mapper.ProviderBodyCheckMapper;
import cn.jy.nvr.mapper.ProviderChestCheckMapper;
import cn.jy.nvr.mapper.ProviderCollectionMapper;
import cn.jy.nvr.mapper.ProviderCostMapper;
import cn.jy.nvr.mapper.ProviderHeatMapper;
import cn.jy.nvr.mapper.ProviderRegisterMapper;
import cn.jy.nvr.mapper.ProviderSampleAssayMapper;
import cn.jy.nvr.utils.TimeUtils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;



@Controller
public class NewProviderController
{
	
	@Autowired
	ProviderBaseInfoMapper baseInfoMapper;
	@Autowired
	ProviderRegisterMapper registerMapper;
	@Autowired 
	ProviderBodyCheckMapper bodyCheckMapper;
	@Autowired 
	ProviderSampleAssayMapper sampleAssayMapper;
	@Autowired
	ProviderChestCheckMapper chestCheckMapper;
	@Autowired
	ProviderAssayMapper assayMapper;
	@Autowired 
	ProviderCollectionMapper collectionMapper;
	@Autowired
	ProviderHeatMapper heatMapper;
	@Autowired
	ProviderCostMapper costMapper;
	@Autowired
	NewProviderMapper newProviderMapper;
	 
	@RequestMapping("display/NewProvider")
	public String Collecting(HttpServletRequest request,HttpServletResponse response){
		 response.setContentType("text/html");
		 response.setCharacterEncoding("utf-8");
		 String callback = request.getParameter("callback"); 
		 
		 String nowDate = request.getParameter("date");
		  List<NewProvider> newProviderList=null;
		 if(!nowDate.isEmpty()){
			//查询缓存表的方式
			 DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
			  newProviderList = newProviderMapper.getNewProviderByDate(nowDate);
					 //直接查询的方式
					  /*  newProviderList = new ArrayList<NewProvider>();
					    
					    List<ProviderBaseInfo> providerBaseInfoList = baseInfoMapper.getProviderBaseInfoByCreateRQ(nowDate);
					    if(providerBaseInfoList.size()>0){
					    	for (ProviderBaseInfo providerBaseInfo : providerBaseInfoList) {
								NewProvider newProvider = new NewProvider();
								if(providerBaseInfo.getID()!=null){
									newProvider.setName(providerBaseInfo.getXM());
									ProviderRegister providerRegister = registerMapper.getProviderRegisterByGJZID(providerBaseInfo.getID(), nowDate);
									if(providerRegister!=null&&providerRegister.getREGTIME()!=null){
										newProvider.setCreateTime(TimeUtils.getIntegerTime(providerRegister.getREGTIME()));
									}
								  ProviderBodyCheck providerBodyCheck = bodyCheckMapper.getProviderBodyCheckByGJZID(providerBaseInfo.getID(), nowDate);
								  if(providerBodyCheck!=null&&providerBodyCheck.getTJJSSJ()!=null){
									  newProvider.setBodyCheckTime(TimeUtils.getIntegerTime(providerBodyCheck.getTJJSSJ()));
								  }
								  ProviderSampleAssay providerSampleAssay = sampleAssayMapper.getProviderSampleAssayByGJZID(providerBaseInfo.getID(), nowDate);
								  if(providerSampleAssay!=null&&providerSampleAssay.getJLJSSJ()!=null){
									  newProvider.setSampleAssayTime(TimeUtils.getIntegerTime(providerSampleAssay.getJLJSSJ()));
								  }
								  ProviderChestCheck providerChestCheck = chestCheckMapper.getProviderChestCheckByGJZID(providerBaseInfo.getID(), nowDate);
								  if(providerChestCheck!=null&&providerChestCheck.getXTSJ()!=null){
									  newProvider.setChestCheckTime(TimeUtils.getIntegerTime(providerChestCheck.getXTSJ()));
								  }
								  ProviderAssay providerAssay = assayMapper.getProviderAssayByGJZID(providerBaseInfo.getID(), nowDate);
								  if(providerAssay!=null&&providerAssay.getJLJSSJ()!=null){
									  newProvider.setAssayTime(TimeUtils.getIntegerTime(providerAssay.getJLJSSJ()));
								  }
								  ProviderCollection providerCollection = collectionMapper.getProviderCollectionByGJZID(providerBaseInfo.getID(), nowDate);
								  if(providerCollection!=null&&providerCollection.getEND_TIME()!=null){
									  newProvider.setCollectionTime(TimeUtils.getIntegerTime(providerCollection.getEND_TIME()));
								  }
								  ProviderCost providerCost = costMapper.getProviderCostByGJZID(providerBaseInfo.getID(), nowDate);
								  if(providerCost!=null&&providerCost.getJSSJ()!=null){
									  newProvider.setCostTime(TimeUtils.getIntegerTime(providerCost.getJSSJ()));
								  }
								  DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
								  ProviderHeat providerHeat=null;
								try {
									providerHeat = heatMapper.getProviderHeatByGJZID(providerBaseInfo.getCARDID(), nowDate);
								} catch (Exception e) {
									e.printStackTrace();
								}
								  if(providerHeat!=null&&providerHeat.getInStorageTime()!=null){
									  newProvider.setHeatTime(TimeUtils.getIntegerTime(providerHeat.getInStorageTime()));
								  }
								  DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
								  newProviderList.add(newProvider);
								}
							}
					    	
					    } */
		 }
		 
		 
		 
		 
		ObjectMapper mapper = new ObjectMapper();
		String json = null;
		try {
			json = mapper.writeValueAsString(newProviderList);
//			System.out.println(json);
		} catch (JsonProcessingException e1) {
			e1.printStackTrace();
		}

		String retStr = callback + "(" + json + ")";
		try {
			PrintWriter out = response.getWriter();
			out.print(retStr);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}